/**
 * User: yuhao
 * Date: 15-2-11
 * Time: 下午4:45
 * 目标市场评估模块的基类，提供了一些模块的通用方法
 */

define(
    [
        "dojo/_base/declare",
        "dojo/_base/array",
        "dojo/topic",
        "dojo/_base/lang",
        "esri/layers/GraphicsLayer",
        "esri/graphic",
        "esri/renderers/SimpleRenderer",
        "../util/symbolUtil"
    ],
    function (declare, array, topic, lang, GraphicsLayer, Graphic, SimpleRenderer, symbolUtil) {

        return declare(null, {

            constructor:function (args) {
                this.map = args.map;
                //当前模块关联的layer
                this.layerIds = [];
            },

            /**
             * 记录当前模块所需的图层id
             * @param ids
             */
            setLayerIds:function (ids) {
                this.layerIds = ids || [];
            },

            /**
             * 获取当前模块所需的图层id
             */
            getLayerIds:function () {
                return this.layerIds;
            },

            /**
             * virtual
             * 初始化当前模块关联的图层
             */
            initLayers:function () {
            },

            /**
             * 清空模块关联的图层
             */
            clearLayers:function (layerId) {
                //清除指定id的layer
                if (layerId) {
                    var layer = this.map.getLayer(layerId);
                    layer.clear();
                }
                //清除全部layer
                else {
                    var layerIds = this.getLayerIds();
                    array.forEach(layerIds, lang.hitch(this, function (id) {
                        var layer = this.map.getLayer(id);
                        layer.clear();
                    }));
                }
            },

            /**
             * 移除当前模块所关联的全部图层
             */
            removeLayers:function () {
                var layersIds = this.getLayerIds();
                array.forEach(layersIds, lang.hitch(this, function (id) {
                    var layer = this.map.getLayer(id);
                    this.map.removeLayer(layer);
                }));
                this.setLayerIds([]);
            },

            /**
             * 退出模块
             * @param callback
             */
            quitModule:function (callback) {
                this.beforeQuit(callback);
            },

            /**
             * virtual
             * 退出当前模块之前的操作
             */
            beforeQuit:function (callback) {
            },

            /**
             * 格式化企业统计数据
             * @param businessStat
             * @return {*}
             */
            formatBusinessResult:function (businessStat) {
                var businessArray = null;
                if (businessStat) {
                    //遍历统计区域
                    businessArray = array.map(businessStat, function (geoInfo) {
                        var tables = geoInfo,
                            attrs = {};
                        //遍历table
                        for (var tableName in tables) {
                            var fields = tables[tableName];
                            //遍历字段
                            for (var fieldName in fields) {
                                var value = fields[fieldName];
                                if (value instanceof Object) {
                                    //遍历分组
                                    var groups = value;
                                    for (var groupName in groups) {
                                        //用分组名+字段名拼成完整的字段名
                                        attrs[groupName + fieldName] = groups[groupName];
                                    }
                                }
                                else {
                                    attrs[fieldName] = value;
                                }
                            }
                        }

                        return attrs;
                    }, this);
                }

                return businessArray;
            },

            /**
             * 合并地理大数据和企业数据统计结果
             * @param features
             * @param geoDataRes
             * @param businessStat
             * @return {Array}
             */
            createFeaturesFromMicroStatResult:function (features, geoDataRes, businessStat) {
                //1.生成企业数据统计结果的array
                var businessArray = this.formatBusinessResult(businessStat);

                //2.遍历全部统计区域，合并地理大数据和企业数据处理结果，两个都没统计出结果的舍掉
                var fList = [];
                for (var i = 0; i < features.length; i++) {
                    var geodataAttrs,
                        businessAttrs;
                    if (geoDataRes)
                        geodataAttrs = geoDataRes[i] ? geoDataRes[i].attributes : null;
                    if (businessArray)
                        businessAttrs = businessArray[i] ? businessArray[i] : null;

                    if (!geodataAttrs && !businessAttrs) {
                        continue;
                    }
                    else {
                        var attrs = features[i].attributes || {};
                        if (geodataAttrs)
                            lang.mixin(attrs, geodataAttrs);
                        if (businessAttrs)
                            lang.mixin(attrs, businessAttrs);
                    }
                    var g = new Graphic(features[i].geometry, null, attrs);
                    fList.push(g);
                }

                return fList;
            },

            /**
             * 生成微观统计的数据
             * @param features
             * @param businessStat
             * @return {Array}
             */
            createMicroReportData:function (features, geoDataRes, businessStat) {
                //1.生成企业数据统计结果的array
                var businessArray = this.formatBusinessResult(businessStat);

                //2.遍历全部统计区域，合并地理大数据和企业数据处理结果，两个都没统计出结果的舍掉
                var reportData = [];
                for (var i = 0; i < features.length; i++) {
                    var geodataAttrs,
                        businessAttrs;
                    if (geoDataRes)
                        geodataAttrs = geoDataRes[i] ? geoDataRes[i].attributes : null;
                    if (businessArray)
                        businessAttrs = businessArray[i] ? businessArray[i] : null;

                    if (!geodataAttrs && !businessAttrs) {
                        continue;
                    }
                    else {
                        var groupAttrs = {};
                        var attrs = features[i].attributes || {};
                        if (geodataAttrs) {
                            lang.mixin(attrs, geodataAttrs);
                            var aliasAttrs = {};
                            for (var name in geodataAttrs) {
                                var alias = this.geoDataManager.getIndex(name).alias;
                                aliasAttrs[alias] = geodataAttrs[name];
                            }
                            groupAttrs["geodata"] = aliasAttrs;
                        }
                        if (businessAttrs) {
                            lang.mixin(attrs, businessAttrs);
                            groupAttrs["business"] = businessAttrs;
                        }
                        reportData.push({
                            "geometry":features[i].geometry,
                            "attributes":groupAttrs
                        });
                    }
                }

                return reportData;
            },

            /**
             * 保存商圈
             * @param args
             */
            toSaveTradeAreas:function (args) {
                //创建layer
                var defaultSymbol = symbolUtil.getColorfulFillSymbol("purple"),
                    render = new SimpleRenderer(defaultSymbol),
                    resultLayer = new GraphicsLayer();
              //存储所有graphic图层
        		//ioc.map.graphiclayers.push(resultLayer);
                resultLayer.setRenderer(render);
                resultLayer.setOpacity(0.5);

                array.forEach(args.features, function (f) {
                    resultLayer.add(f);
                });
                //通知"可视化"
                var params = {
                    layer:resultLayer,
                    name:args.layerName,
                    fields:args.fields,
                    popup:args.popup,
                    type:{
                        group:"layer",
                        name:args.layerType
                    }
                };
                topic.publish("layerCreated", params);
            }
        });
    });